<!--下订单已选商品-->
<style lang="less" scoped>
.list-goods-warp{
  border: none;
  &:active{
    background: #fff;
  }
  &:focus{
    background: #fff;
  }
  
}
</style>

<template>
  <div class="contain">
    <div class="mb30" v-for="item in checkItem">
      <!-- <div class="detail-supply"><i class="iconfont icon-supplier ft17"></i>
        武汉未来医药供应商
      </div> -->
      <div class="displayflex list-goods-warp">
        <div class="pic">
          <img :src="item.PictContent | filterImgUrl">
        </div>
        <div class="msg flex1">
          <p class="ft17">{{item.GoodsName}}</p>
          <p>{{item.GoodsSpec}}</p>
          <p>{{item.Producer}}</p>
          <p class="org1 ft17">￥{{item.Price | filterPrice}}/{{item.MinUnitStyle}}</p>
        </div>
      </div>
      <div class="cf choiced-bot">
        <div class="f-l org1 ft16" @click="delChecked(item)"><i class="iconfont icon-delete2"></i>删除</div>
        <div class="num-selector displayflex f-r">
          <i class="iconfont icon-reduce" @click="changeNum(item,-1)"></i>
          <input number type="text" class="flex1" placeholder="0" v-model="item.PurchaserPoQty" @input="iptBlur(item)">
          <i class="iconfont icon-add" @click="changeNum(item,1)"></i>
        </div>
      </div>
    </div>  
    
    <div class="detail-bottom displayflex">
      <span>合计：<em class="red">￥{{amount}}</em></span>
      <div class="flex1 btn-grp">
        <div @click="clearItems" class="btn red">清空</div>
        <div @click="checkedItem" class="btn ft16">确定</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "placeChoiced",
  data() {
    return {
      checkItem: [],
      amount: ""
    };
  },
  created() {
    this.getDate();
  },
  methods: {
    delChecked(val) {
      for (var i = 0; i < this.checkItem.length; i++) {
        if (val.Guid == this.checkItem[i].Guid) {
          this.checkItem.splice(i, 1);
        }
      }
      this.amountNum();
      sessionStorage.goods = JSON.stringify(this.checkItem);
    },
    //加减
    changeNum: function(product, way) {
      if (way > 0) {
        product.PurchaserPoQty++;
      } else if (way < 0 && product.PurchaserPoQty > 0) {
        product.PurchaserPoQty--;
      } else {
        this.Toast({
          message: "请输入大于0的数"
        });
      }
      if (product.checkedshow) {
        this.amountNum();
      }
    },
    //数量输入框
    iptBlur(val) {
      if (!isNaN(val.PurchaserPoQty)) {
        val.PurchaserPoQty = parseInt(val.PurchaserPoQty);
      } else {
        val.PurchaserPoQty = 0;
      }
      if (val.checkedshow) {
        this.amountNum();
      }
    },
    amountNum() {
      this.amount = 0;
      for (var i = 0; i < this.checkItem.length; i++) {
        var price = this.checkItem[i].Price || 0;
        var qty = this.checkItem[i].PurchaserPoQty || 0;
        this.amount += price * parseInt(qty);
      }
    },
    getDate() {
      var self = this;
      if (sessionStorage.goods) {
        this.checkItem = JSON.parse(sessionStorage.goods);
      }
      this.amountNum();
    },
    checkedItem() {
      var value = this.checkItem;
      for (var i = 0; i < value.length; i++) {
        if (!value[i].PurchaserPoQty || value[i].PurchaserPoQty == "0") {
          this.Toast({
            message: "选择的商品数量不能为零"
          });
          return false;
        }
      }
      sessionStorage.goods = JSON.stringify(this.checkItem);
      var userinfo = JSON.parse(localStorage.userinfo);
      if (userinfo.UserBizType == "Hospital") {
        this.$router.replace({
          path: "/order/placeCusOrder"
        });
      } else {
        this.$router.replace({
          path: "/order/placeOrder"
        });
      }
    },
    clearItems(){
      if(this.checkItem.length <= 0 ){
        this.Toast({
          message: "商品清单是空的",
        });
      } else {
        this.MessageBox.confirm('确定清空商品清单吗？').then(action => {
          this.checkItem = [];
          sessionStorage.goods = JSON.stringify(this.checkItem);
          this.$router.go(-1);
        });
      }

    }
  },
  filters: {
    //价格
    filterPrice(val) {
      return val ? val.toFixed(2) : 0;
    }
  }
};
</script>